const APIKEY = "b25aabebe385483faf14c0dc902b339e";// 填入你申请的KEY
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getLocation()
    },
    //选择定位
    selectLocation() {
        var that = this
        wx.chooseLocation({
            success(res) {
                that.setData({
                    location: res.longitude + "," + res.latitude
                })
                that.getWeather()
                that.getCityByLoaction()
            }
            , fail() {
                wx.getLocation({
                    type: 'gcj02',
                    fail() {
                        wx.showModal({
                            title: '获取地图位置失败',
                            content: '为了给您提供准确的天气预报服务,请在设置中授权【位置信息】',
                            success(mRes) {
                                if (mRes.confirm) {
                                    wx.openSetting({
                                        success: function (data) {
                                            if (data.authSetting["scope.userLocation"] === true) {
                                                that.selectLocation()
                                            } else {
                                                wx.showToast({
                                                    title: '授权失败',
                                                    icon: 'none',
                                                    duration: 1000
                                                })
                                            }
                                        }, fail(err) {
                                            console.log(err)
                                            wx.showToast({
                                                title: '唤起设置页失败，请手动打开',
                                                icon: 'none',
                                                duration: 1000
                                            })
                                        }
                                    })
                                }
                            }
                        })
                    }
                })

            }
        })
    },
    /**
     * 获取定位
     */
    getLocation() {
        var that = this
        wx.getLocation({
            type: 'gcj02',
            success(res) {
                that.setData({
                    location: res.longitude + "," + res.latitude
                })
                that.getWeather()
                that.getCityByLoaction()
            }, fail(err) {
                wx.showModal({
                    title: '获取定位信息失败',
                    content: '为了给您提供准确的天气预报服务,请在设置中授权【位置信息】',
                    success(mRes) {
                        if (mRes.confirm) {
                            wx.openSetting({
                                success: function (data) {
                                    if (data.authSetting["scope.userLocation"] === true) {
                                        wx.showToast({
                                            title: '授权成功',
                                            icon: 'success',
                                            duration: 1000
                                        })
                                        that.getLocation()
                                    } else {
                                        wx.showToast({
                                            title: '授权失败',
                                            icon: 'none',
                                            duration: 1000
                                        })
                                        that.setData({
                                            location: "116.41,39.92"
                                        })
                                        that.getWeather()
                                        that.getCityByLoaction()
                                    }
                                }, fail(err) {
                                    console.log(err)
                                    wx.showToast({
                                        title: '唤起设置页失败，请手动打开',
                                        icon: 'none',
                                        duration: 1000
                                    })
                                    that.setData({
                                        location: "116.41,39.92"
                                    })
                                    that.getWeather()
                                    that.getCityByLoaction()
                                }
                            })
                        } else if (mRes.cancel) {
                            that.setData({
                                location: "116.41,39.92"
                            })
                            that.getWeather()
                            that.getCityByLoaction()
                        }
                    }
                })
            }
        })
    },
    /**
     * 根据坐标获取城市信息
     */
    getCityByLoaction() {
        var that = this
        wx.request({
            url: 'https://geoapi.qweather.com/v2/city/lookup?key=' + APIKEY + "&location=" + that.data.location,
            success(result) {
                var res = result.data
                if (res.code == "200") {
                    var data = res.location[0]
                    that.setData({
                        City: data.adm2,
                        County: data.name
                    })
                } else {
                    wx.showToast({
                        title: '获取城市信息失败',
                        icon: 'none'
                    })
                }

            }
        })
    },
    /**
     * 获取天气
     */
    getWeather() {
        var that = this
        wx.showLoading({
            title: '加载中',
        })
        wx.request({
            url: 'https://devapi.qweather.com/v7/weather/now?key=' + APIKEY + "&location=" + that.data.location,
            success(result) {
                var res = result.data

                if (res.now.feelsLike >= 30) {
                    res.now.wind360 = "今天天气很好，适宜T恤，短裤"
                }
                else if (res.now.feelsLike >= 20) {
                    res.now.wind360 = "初春的天气很好，适宜单卦、薄裤"
                }
                else if (res.now.feelsLike >= 10) {
                    res.now.wind360 = "似乎到了秋天，适宜添上外套"
                }
                else if (res.now.feelsLike >= 0) {
                    res.now.wind360 = "宛如初冬，料峭寒，适宜薄袄"
                }
                else {
                    res.now.wind360 = "凛冬已至，羽绒服快快出现"
                }
                that.setData({
                    now: res.now
                })
            }
        })
        wx.request({
            url: 'https://devapi.qweather.com/v7/weather/24h?key=' + APIKEY + "&location=" + that.data.location,
            success(result) {
                var res = result.data
                res.hourly.forEach(function (item) {
                    item.time = that.formatTime(new Date(item.fxTime)).hourly
                })
                that.setData({
                    hourly: res.hourly
                })
            }
        })
        wx.request({
            url: 'https://devapi.qweather.com/v7/weather/7d?key=' + APIKEY + "&location=" + that.data.location,
            success(result) {
                var res = result.data
                res.daily.forEach(function (item) {
                    item.date = that.formatTime(new Date(item.fxDate)).daily
                    item.dateToString = that.formatTime(new Date(item.fxDate)).dailyToString
                })
                that.setData({
                    daily: res.daily
                })
                wx.hideLoading()
            }
        })
    },
    // 格式时间
    formatTime(date) {
        const hour = date.getHours()
        const minute = date.getMinutes()
        const second = date.getSeconds()
        const isToday = date.setHours(0, 0, 0, 0) == new Date().setHours(0, 0, 0, 0)
        return {
            hourly: [hour, minute].map(this.formatNumber).join(":"),
        }
    },
    // 补零
    formatNumber(n) {
        n = n.toString()
        return n[1] ? n : '0' + n
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})

